%%% -------------------------------------------------------------------
%%%
%%% http://www.9miao.com
%%% -------------------------------------------------------------------
%%% -------------------------------------------------------------------
%%% Author  : PCWS06
%%% Description :
%%%
%%% Created : 2010-7-8
%%% -------------------------------------------------------------------
-module(chat_manager_sup).

-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([start_link/0]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([
	 init/1
        ]).


-export([start_chat_role/1,stop_role/2]).
%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------
-define(SERVER, ?MODULE).

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% ====================================================================
%% External functions
%% ====================================================================

start_link() ->
	supervisor:start_link({local,?MODULE},?MODULE, []).

%% ====================================================================
%% Server functions
%% ====================================================================
%% --------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok,  {SupFlags,  [ChildSpec]}} |
%%          ignore                          |
%%          {error, Reason}
%% --------------------------------------------------------------------
init([]) ->
    AChild = {chat_manager,{chat_manager,start_link,[]},
	      permanent,2000,worker,[chat_manager]},
    {ok,{{one_for_one,10,10}, [AChild]}}.

%% ====================================================================
%% Internal functions
%% ====================================================================
%% create chat role
start_chat_role(RoleId) ->	
	ChildSpec= {RoleId,{chat_process,start_link,[RoleId]},
		    permanent, 2000, worker,[chat_processor]},
	supervisor:start_child(?SERVER, ChildSpec).

stop_role(RoleSupNode, RoleId)->
	supervisor:terminate_child({?SERVER,RoleSupNode} , RoleId),
	supervisor:delete_child({?SERVER, RoleSupNode}, RoleId).
